/*
 * @(#)ListContactGroupAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.contact.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.contact.bean.ContactList;
import com.dcivision.contact.bean.ContactPerson;
import com.dcivision.contact.bean.MtmContactListObjectLink;
import com.dcivision.contact.dao.ContactListDAObject;
import com.dcivision.contact.dao.ContactPersonDAObject;
import com.dcivision.contact.dao.MtmContactListObjectLinkDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.bean.MtmUserGroupUserRole;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.bean.UserRole;
import com.dcivision.user.dao.MtmUserGroupUserRoleDAObject;
import com.dcivision.user.dao.MtmUserRecordUserGroupDAObject;
import com.dcivision.user.dao.MtmUserRecordUserRoleDAObject;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRecordDAObject;
import com.dcivision.user.dao.UserRoleDAObject;

/**
  ListContactGroupAction.java

  This class is the data access bean for table "CONTACT_PERSON".

  @author           Jenny Li
  @company          DCIVision Limited
  @creation date    04/01/2005
  @version          $Revision: 1.12 $
*/

public class ListContactGroupAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.12 $";


  /**
  *  Constructor - Creates a new instance of ListContactCompanyAction and define the default listName.
  */
  public ListContactGroupAction() {
    super();
    this.setListName("contactGroupList");
  }

  /**
  * getMajorDAOClassName
  * 
  * @return  The class name of the major DAObject will be used in this action.
  */
  public String getMajorDAOClassName() {
    return("com.dcivision.contact.dao.ContactListDAObject");
  }

  /**
  * getFunctionCode
  *
  * @return  The corresponding system function code of action.
  */
  public String getFunctionCode() {
    return(SystemFunctionConstant.CONTACT_GROUP);
  }
  
  /**
   *  Method getListData() - retrieve the appropriate item list for current page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws Exception if cannot get the list data from DAO object
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    List list=new ArrayList();
    List userList=new ArrayList();
    List groupList=new ArrayList();
    List roleList=new ArrayList();
    List exteralContactList=new ArrayList();
    List userRecordIDs=new ArrayList();
    List userGroupIDs=new ArrayList();
    List userRoleIDs=new ArrayList();
    
    ContactListDAObject contactListDAO=new ContactListDAObject(sessionContainer,conn);
    MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
    MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO=new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
    MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO=new MtmUserGroupUserRoleDAObject(sessionContainer,conn);
    ListContactGroupForm listForm=(ListContactGroupForm)form;
    
    // handler search by group/role
    if(!Utility.isEmpty(listForm.getUserRoleID())){
      userRoleIDs.add(0,TextUtility.parseIntegerObj(listForm.getUserRoleID()));  
    }
    if(!Utility.isEmpty(listForm.getUserGroupID()) && Utility.isEmpty(listForm.getUserRoleID())){
      userRecordIDs=mtmUserRecordUserGroupDAO.getUserIDListByUserGroupID(TextUtility.parseIntegerObj(listForm.getUserGroupID()));
    }else if(!Utility.isEmpty(listForm.getUserRoleID()) && Utility.isEmpty(listForm.getUserGroupID())){
      userRecordIDs=mtmUserRecordUserRoleDAO.getUserIDListByUserRoleID(TextUtility.parseIntegerObj(listForm.getUserRoleID()));
      List groupRoleList=mtmUserGroupUserRoleDAO.getListByUserRoleAry(userRoleIDs);
      for(int i=0;i<groupRoleList.size();i++){
        MtmUserGroupUserRole tmpMtmUserGroupUserRole = (MtmUserGroupUserRole)groupRoleList.get(i);
        userGroupIDs.add(tmpMtmUserGroupUserRole.getUserGroupID());
      }
    }else if(!Utility.isEmpty(listForm.getUserGroupID()) && !Utility.isEmpty(listForm.getUserRoleID())){
      List groupUserList=mtmUserRecordUserGroupDAO.getUserIDListByUserGroupID(TextUtility.parseIntegerObj(listForm.getUserGroupID()));
      List roleUserList=mtmUserRecordUserRoleDAO.getUserIDListByUserRoleID(TextUtility.parseIntegerObj(listForm.getUserRoleID()));
      for(int i=0;i<groupUserList.size();i++){
        roleUserList.contains(groupUserList.get(i));
        userRecordIDs.add(groupUserList.get(i));
      }  
    }
    
    //get search list
    list=contactListDAO.getAllList(form,userRecordIDs,userGroupIDs);
    for(int i=0;i<list.size();i++){
      ContactList contactList=(ContactList)list.get(i); 
      if(ContactList.SHARING_TYPE_PUB.equals(contactList.getPublicFlag())){
        contactList.setPublicFlag("public"); 
      }else {
        contactList.setPublicFlag("private");  
      }     
      userList.add(this.getContactList(MtmContactListObjectLink.OBJECT_TYPE_USER_RECORD,contactList.getID(),request));
      groupList.add(this.getContactList(MtmContactListObjectLink.OBJECT_TYPE_USER_GROUP,contactList.getID(),request));
      roleList.add(this.getContactList(MtmContactListObjectLink.OBJECT_TYPE_USER_ROLE,contactList.getID(),request));
      exteralContactList.add(this.getContactList(MtmContactListObjectLink.OBJECT_TYPE_CONTACT_PERSON,contactList.getID(),request));     
    }
    request.setAttribute("listUser",userList);
    request.setAttribute("listGroup",groupList);
    request.setAttribute("listRole",roleList);
    request.setAttribute("listExternalContact",exteralContactList);

    request.setAttribute(this.getListName(),list);
  }
  
  public void deleteListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.deleteListData(mapping, form,request,response);
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    MtmContactListObjectLinkDAObject mtmContactListObjectLinkDAO=new MtmContactListObjectLinkDAObject(sessionContainer,conn);
    
    for(int i=0;i<form.getBasicSelectedID().length;i++){
      Integer selectedID = TextUtility.parseIntegerObj(form.getBasicSelectedID()[i]);  
      mtmContactListObjectLinkDAO.deleteByContactListID(selectedID);     
    }
  }
  
  protected List getContactList(String objectType,Integer contactListID,HttpServletRequest request) throws ApplicationException{
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    List newList=new ArrayList();
    UserRecordDAObject userRecordDAO=new UserRecordDAObject(sessionContainer,conn);
    UserGroupDAObject userGroupDAO=new UserGroupDAObject(sessionContainer,conn);
    UserRoleDAObject userRoleDAO=new UserRoleDAObject(sessionContainer,conn);
    ContactPersonDAObject contactPersonDAO=new ContactPersonDAObject(sessionContainer,conn);
    MtmContactListObjectLinkDAObject mtmContactListObjectLinkDAO=new MtmContactListObjectLinkDAObject(sessionContainer,conn);
    
    List list=mtmContactListObjectLinkDAO.getListByObjectType(objectType,contactListID);
    for(int i=0;i<list.size();i++){
      try{
        MtmContactListObjectLink mtmContactListObjectLink=(MtmContactListObjectLink)list.get(i);
        if(MtmContactListObjectLink.OBJECT_TYPE_USER_RECORD.equals(objectType)){
          UserRecord userRecord=(UserRecord)userRecordDAO.getObjectByID(mtmContactListObjectLink.getObjectID());
          newList.add(userRecord);
        }else if(MtmContactListObjectLink.OBJECT_TYPE_USER_GROUP.equals(objectType)){
          UserGroup userGroup=(UserGroup)userGroupDAO.getObjectByID(mtmContactListObjectLink.getObjectID());
          newList.add(userGroup);
        }else if(MtmContactListObjectLink.OBJECT_TYPE_USER_ROLE.equals(objectType)){
          UserRole userRole=(UserRole)userRoleDAO.getObjectByID(mtmContactListObjectLink.getObjectID());
          newList.add(userRole);
        }else if(MtmContactListObjectLink.OBJECT_TYPE_CONTACT_PERSON.equals(objectType)){
          ContactPerson contactPerson=(ContactPerson)contactPersonDAO.getObjectByID(mtmContactListObjectLink.getObjectID());
          contactPerson.setFullName(contactPerson.getFirstName()+" "+contactPerson.getLastName());
          newList.add(contactPerson);
        }
      }catch(ApplicationException appx){
        if(!ErrorConstant.DB_RECORD_NOT_FOUND_ERROR.equals(appx.getMsgCode())){
          throw appx;
        }
      }
    }
    return newList;
  } 
  
  
}
